<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=`, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <title>boost search enginee</title>
    <style>
        /*去掉内外边距*/
        *{
            /* 设置外边距 */
            margin: 0;
            /* 设置内边距 */
            padding: 0;
        }
        html, 
        body{
            height: 100%;
        }

        /* 类选择器.container */
        .container { 
            width: 800px;
            /*  设置外边距，居中对齐*/
            margin: 0px auto;
            margin-top: 15px;
        }
        
        /* 复合选择器，选中container下的search */
        .container .search {
            /* 宽度和父标签保持一致 */
            width: 100%;
            height: 52px;

        }

        /* 选中input标签，设置标签属性 */
        .container .search input {
            float: left; /*粗糙的拼接*/
            width: 280px;
            height: 28px;

            /* 字体颜色和大小 */
            font-size: 17px;
            color: grey;

            /* 边框样式 ：去掉右边框*/
            border: 2px solid gainsboro;
            border-right: none;
            border-top-left-radius: 5px;
            border-bottom-left-radius: 5px;

            /* 内边距：让文字有距离 */
            padding-left: 10px;
        }

        .container .search button {
            float: left;
            width: 120px;
            height: 31px;

            font-size: 17px;
            color: #fff;

            background-color:green;
            border: 1px solid green;
            border-top-right-radius: 5px;
            border-bottom-right-radius: 5px;
        }

        .container .results {
            width: 100%;

            margin-top: 15px;
        }

        .container .results .item a{
            /* 设置a为块级元素，单占一行 */
            display: block;
            text-decoration: none;
            font-size: 20px;
            color: rgba(0, 128, 0, 0.845);
        }

        .container .results .item a:hover {
            /* 设置鼠标动态效果 */
            text-decoration: underline;
        }
        
        .container .results .item p {
            font-size: 16px;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin-top: 4px;
        }

        .container .results .item i {
            /* 取消斜体 */
            display: block;
            font-style: normal;
            color: blue;

        }
    </style>
</head>

<body>
    <div class="container"> 
        <div class="search">
            <input type="text" value="enchanted by bts">
            <button onclick="Search()">Go</button>
        </div>
        <div class="results"> 
            <!-- <div class="item">
                <a href="#"> 标题 </a>
                <p> desc </p>
                <i> url </i>
            </div> -->
        </div>
    </div>

    <script>
        function Search() {
            // 浏览器弹出框
            // 1. 提取数据 JQuery
            let query = $(".container .search input").val();
            console.log("query = "+query);

            // 2. 发起http请求，JQuery中和后端交互的函数
            $.ajax({
                type: "GET",
                url: "/s?word=" +query,
                success: function(data) {
                    console.log(data);
                    BuildHtml(data);
                }
            });
        }

        function BuildHtml(data) {
            // 获取html的result标签
            let results_label = $(".container .results");
            // 清空历史搜索结果
            results_label.empty();

            for(let elem of data) {
                // console.log(elem.title);
                // console.log(elem.url);
                let a_label = $("<a>",{
                    text: elem.title,
                    href: elem.url,
                    target: "_blank" //跳转到新的页面
                });

                let p_label = $("<p>", {
                    text: elem.desc
                });

                let i_label = $("<i>", {
                    text:elem.url
                });

                let div_label = $("<div>", {
                    class: "item"
                });

                a_label.appendTo(div_label);
                p_label.appendTo(div_label);
                i_label.appendTo(div_label);

                div_label.appendTo(results_label);
            }
        }
        
    </script>
</body>
</html>

